﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DVSProject.Lib;
using DVSProject.Models;

namespace DVSProject.Controllers
{
    public class ProcedureRemarkController : Controller
    {
        //
        // GET: /ProcedureRemark/

        public ActionResult Index()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ProcedureRemarkRepo _ProcedureRemarkRepo = new ProcedureRemarkRepo();
            IEnumerable<ProcedureRemarkModels> _ProcedureRemarks = _ProcedureRemarkRepo.GetProcedureRemark();
            return View(_ProcedureRemarks);
        }

        //
        // GET: /ProcedureRemark/Details/5

        public ActionResult Details(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ProcedureRemarkRepo _ProcedureRemarkRepo = new ProcedureRemarkRepo();
            ProcedureRemarkModels _ProcedureRemark = new ProcedureRemarkModels();
            _ProcedureRemark = _ProcedureRemarkRepo.GetProcedureRemarkById(id);
            return View(_ProcedureRemark);
        }

        //
        // GET: /ProcedureRemark/Create

        public ActionResult Create()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ProcedureRemarkModels _ProcedureRemark = new ProcedureRemarkModels();

            TitleProcedureRepo _TitleProcedureRepo = new TitleProcedureRepo();
            IEnumerable<TitleProcedureModels> _TitleProcedures = _TitleProcedureRepo.GetTitleProcedure();
            int selectedId = 1;
            IEnumerable<SelectListItem> selectlist = from s in _TitleProcedures
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.TitleProcedureId == selectedId),
                                                         Text = s.TitleProcedureName,
                                                         Value = s.TitleProcedureId.ToString()
                                                     };

            _ProcedureRemark.TitleSelect = selectlist;
            return View(_ProcedureRemark);
        } 

        //
        // POST: /ProcedureRemark/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add insert logic here
                ProcedureRemarkRepo _ProcedureRemarkRepo = new ProcedureRemarkRepo();
                ProcedureRemarkModels _ProcedureRemark = new ProcedureRemarkModels();

                _ProcedureRemark.TitleProcedureId = (!String.IsNullOrEmpty(collection["TitleProcedureId"]))? Convert.ToInt32(collection["TitleProcedureId"]):0;
                _ProcedureRemark.ProcedureRemarkTh = collection["ProcedureRemarkTh"];
                _ProcedureRemark.ProcedureRemarkEng = collection["ProcedureRemarkEng"];
                _ProcedureRemark.CreateBy = (string)Session["User"];

                bool result = _ProcedureRemarkRepo.Create(_ProcedureRemark);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /ProcedureRemark/Edit/5
 
        public ActionResult Edit(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ProcedureRemarkRepo _ProcedureRemarkRepo = new ProcedureRemarkRepo();
            ProcedureRemarkModels _ProcedureRemark = new ProcedureRemarkModels();
            _ProcedureRemark = _ProcedureRemarkRepo.GetProcedureRemarkById(id);

            TitleProcedureRepo _TitleProcedureRepo = new TitleProcedureRepo();
            IEnumerable<TitleProcedureModels> _TitleProcedures = _TitleProcedureRepo.GetTitleProcedure();
            int selectedId = 1;
            IEnumerable<SelectListItem> selectlist = from s in _TitleProcedures
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.TitleProcedureId == selectedId),
                                                         Text = s.TitleProcedureName,
                                                         Value = s.TitleProcedureId.ToString()
                                                     };

            //_Machine.ModelMacs = _Models;
            _ProcedureRemark.TitleSelect = selectlist;
            return View(_ProcedureRemark);
        }

        //
        // POST: /ProcedureRemark/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here
                ProcedureRemarkRepo _ProcedureRemarkRepo = new ProcedureRemarkRepo();
                ProcedureRemarkModels _ProcedureRemark = new ProcedureRemarkModels();
                _ProcedureRemark.Id = id;
                _ProcedureRemark.TitleProcedureId = (!String.IsNullOrEmpty(collection["TitleProcedureId"])) ? Convert.ToInt32(collection["TitleProcedureId"]) : 0;
                _ProcedureRemark.ProcedureRemarkTh = collection["ProcedureRemarkTh"];
                _ProcedureRemark.ProcedureRemarkEng = collection["ProcedureRemarkEng"];
                _ProcedureRemark.UpdateBy = (string)Session["User"];
                bool result = _ProcedureRemarkRepo.Edit(_ProcedureRemark);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /ProcedureRemark/Delete/5
 
        public ActionResult Delete(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ProcedureRemarkRepo _ProcedureRemarkRepo = new ProcedureRemarkRepo();
            ProcedureRemarkModels _ProcedureRemark = new ProcedureRemarkModels();
            _ProcedureRemark = _ProcedureRemarkRepo.GetProcedureRemarkById(id);
            return View(_ProcedureRemark);
        }

        //
        // POST: /ProcedureRemark/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add delete logic here
                ProcedureRemarkRepo _ProcedureRemarkRepo = new ProcedureRemarkRepo();
                ProcedureRemarkModels _ProcedureRemark = new ProcedureRemarkModels();
                _ProcedureRemark.Id = id;
                _ProcedureRemark.UpdateBy = (string)Session["User"];
                bool result = _ProcedureRemarkRepo.Delete(_ProcedureRemark);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
